#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QMenu>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->tBtnListInit->setDefaultAction(ui->actListInit);
    ui->tBtnListClear->setDefaultAction(ui->actListClear);
    ui->tBtnListInsert->setDefaultAction(ui->actItemInsert);
    ui->tBtnListAppend->setDefaultAction(ui->actItemAppend);
    ui->tBtnListDelete->setDefaultAction(ui->actItemDelete);

    ui->tBtnSelAll->setDefaultAction(ui->actSelAll);
    ui->tBtnSelNone->setDefaultAction(ui->actSelNone);
    ui->tBtnSelInvs->setDefaultAction(ui->actSelInvs);

    QMenu *menuSelection = new QMenu(this);
    menuSelection->addAction(ui->actSelAll);
    menuSelection->addAction(ui->actSelNone);
    menuSelection->addAction(ui->actSelInvs);
    // 将菜单挂到toolButton按钮上
    ui->tBtnSelItem->setPopupMode(QToolButton::MenuButtonPopup);
    ui->tBtnSelItem->setMenu(menuSelection);

    QToolButton *tBtnSelectOption = new QToolButton(this);
    tBtnSelectOption->setPopupMode(QToolButton::InstantPopup);
    tBtnSelectOption->setText("选择选项");
    tBtnSelectOption->setIcon(QIcon(":/icons/images/option.png"));
    tBtnSelectOption->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    tBtnSelectOption->setMenu(menuSelection);

    ui->toolBar->addWidget(tBtnSelectOption);
    ui->toolBar->addSeparator();
    ui->toolBar->addAction(ui->actQuit);

    ui->listWidget->setSortingEnabled(ui->chkBoxSorting->isChecked());
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actListInit_triggered()
{
    QListWidgetItem *item;
    ui->listWidget->clear();
    QIcon icon;
    icon.addFile(":/icons/images/circle.png");

    bool chk = ui->chkBoxListEditable->isChecked();

    // Create items for listWidget
    for (int i = 0; i < 10; ++i) {
        item = new QListWidgetItem();
        item->setText("Item" + QString::number(i));
        item->setIcon(icon);
        item->setCheckState(Qt::Checked);

        if (chk) {
            item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable
                           | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        }
        else {
            item->setFlags(Qt::ItemIsSelectable
                           | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        }

        ui->listWidget->addItem(item);
    }
}


void MainWindow::on_actListClear_triggered()
{
    ui->listWidget->clear();
}


void MainWindow::on_actItemInsert_triggered()
{
    QIcon icon(":/icons/images/circle.png");
    QListWidgetItem *item = new QListWidgetItem();
    item->setText("Item inserted");
    item->setIcon(icon);
    item->setCheckState(Qt::Checked);

    bool chk = ui->chkBoxListEditable->isChecked();
    if (chk) {
        item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable
                       | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    else {
        item->setFlags(Qt::ItemIsSelectable
                       | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }

    ui->listWidget->insertItem(ui->listWidget->currentRow(), item);
}


void MainWindow::on_actItemAppend_triggered()
{
    QIcon icon(":/icons/images/circle.png");
    QListWidgetItem *item = new QListWidgetItem();
    item->setText("Item added");
    item->setIcon(icon);
    item->setCheckState(Qt::Checked);

    bool chk = ui->chkBoxListEditable->isChecked();
    if (chk) {
        item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable
                       | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    else {
        item->setFlags(Qt::ItemIsSelectable
                       | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }

    ui->listWidget->addItem(item);
}


void MainWindow::on_actItemDelete_triggered()
{
    int row = ui->listWidget->currentRow();
    delete ui->listWidget->takeItem(row);
}


void MainWindow::on_actSelAll_triggered()
{
    int count = ui->listWidget->count();

    for (int i = 0; i < count; ++i) {
        QListWidgetItem *item = ui->listWidget->item(i);
        if (item->checkState() != Qt::Checked)
            item->setCheckState(Qt::Checked);
    }
}


void MainWindow::on_actSelNone_triggered()
{
    int count = ui->listWidget->count();

    for (int i = 0; i < count; ++i) {
        QListWidgetItem *item = ui->listWidget->item(i);
        if (item->checkState() != Qt::Unchecked)
            item->setCheckState(Qt::Unchecked);
    }
}


void MainWindow::on_actSelInvs_triggered()
{
    int count = ui->listWidget->count();

    for (int i = 0; i < count; ++i) {
        QListWidgetItem *item = ui->listWidget->item(i);
        if (item->checkState() == Qt::Checked)
            item->setCheckState(Qt::Unchecked);
        else
            item->setCheckState(Qt::Checked);
    }
}


void MainWindow::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    QString str;
    if (current != nullptr) {
        if (previous == nullptr) {
            str = "当前项：" + current->text();
        }
        else {
            str = "前一项：" + previous->text() + "；当前项：" + current->text();
        }

         ui->editCurItemText->setText(str);
    }

    ui->plainTextEdit->appendPlainText("currentItemChanged()信号被发射");

}


void MainWindow::on_chkBoxSorting_clicked(bool checked)
{
    ui->listWidget->setSortingEnabled(checked);
}


void MainWindow::on_tBtnSortAsc_clicked()
{
    if (ui->listWidget->isSortingEnabled())
        ui->listWidget->sortItems(Qt::AscendingOrder);
}


void MainWindow::on_tBtnSortDes_clicked()
{
    if (ui->listWidget->isSortingEnabled())
        ui->listWidget->sortItems(Qt::DescendingOrder);
}


void MainWindow::on_tBtnClearText_clicked()
{
    ui->plainTextEdit->clear();
}


void MainWindow::on_tBtnAddLine_clicked()
{
    ui->plainTextEdit->appendPlainText("");
}


void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    ui->plainTextEdit->appendPlainText("itemDoubleClicked: " + item->text());
}


void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
    Q_UNUSED(pos); // to avoid warning

    QMenu *menu = new QMenu();
    menu->addAction(ui->actListInit);
    menu->addAction(ui->actListClear);
    menu->addAction(ui->actItemInsert);
    menu->addAction(ui->actItemAppend);
    menu->addAction(ui->actItemDelete);
    menu->addSeparator();
    menu->addAction(ui->actSelAll);
    menu->addAction(ui->actSelNone);
    menu->addAction(ui->actSelInvs);

    menu->exec(QCursor::pos());
    delete menu;
}

